/*
 * @lc app=leetcode.cn id=114 lang=cpp
 *
 * [114] 二叉树展开为链表
 */

// @lc code=start
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution
{
public:
  void flatten(TreeNode* root)
  {
    vector<TreeNode*> nodes;
    DFS(root, nodes);
    TreeNode* dummy = new TreeNode(-1);
    TreeNode* tmp = dummy;
    for (int i = 0; i < nodes.size(); ++i) {
      tmp->right = nodes[i];
      tmp = tmp->right;
      tmp->left = nullptr;
    }
  }

  void DFS(TreeNode* root, vector<TreeNode*>& nodes)
  {
    if (root) {
      nodes.push_back(root);
      DFS(root->left, nodes);
      DFS(root->right, nodes);
    }
  }
};
// @lc code=end
